package com.aw.guiguzi.module.manager.transaction

import android.view.View
import com.aw.baselib.base.AwBaseBindingActivity
import com.aw.baselib.base.AwBaseEvent
import com.aw.baselib.constants.Extras
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwGsonUtil
import com.aw.baselib.util.AwLog
import com.aw.baselib.util.AwRecyclerViewUtil
import com.aw.guiguzi.base.MyApp
import com.aw.guiguzi.bean.TransactionItemBean
import com.aw.guiguzi.databinding.ActivityManagerTransactionBinding
import com.aw.guiguzi.event.EventRefreshTransactionDataType
import com.aw.guiguzi.event.EventRefreshTransactionListType
import com.aw.guiguzi.module.common.CommonImgActivity
import com.aw.guiguzi.module.manager.transaction.adapter.ManagerTransactionAdapter
import com.aw.guiguzi.util.AppUserUtil.userRemark
import com.chad.library.adapter.base.BaseQuickAdapter
import com.google.gson.reflect.TypeToken

class ManagerTransactionActivity : AwBaseBindingActivity<ActivityManagerTransactionBinding?>() {
    private var mlist: MutableList<TransactionItemBean> = ArrayList()
    private var mAdapter: ManagerTransactionAdapter? = null
    override fun refreshByBus(type: AwBaseEvent) {
        super.refreshByBus(type)
        if (type is EventRefreshTransactionListType) {
            if (type.getObj() != null) {
                mlist = type.getObj() as MutableList<TransactionItemBean>
            }
            mAdapter!!.addAllData(mlist)
        } else if (type is EventRefreshTransactionDataType) {
            mlist = AwGsonUtil.fromJson(
                MyApp.transactionData,
                object : TypeToken<List<TransactionItemBean?>?>() {}.type
            )
            mAdapter!!.addAllData(mlist)
        }
    }

    override fun getViewBinding(): ActivityManagerTransactionBinding {
        return ActivityManagerTransactionBinding.inflate(layoutInflater)
    }

    override fun initView() {
        super.initView()
        setToolbarWithBackImgAndRightView("交易管理", "设置") {
            toClass(
                ManagerTransactionSettingActivity::class.java, false
            )
        }
    }

    override fun initData() {
        super.initData()
        mAdapter = ManagerTransactionAdapter()
        AwRecyclerViewUtil.setRecyclerViewLinearlayout(
            mActivity,
            binding!!.rcvData,
            mAdapter,
            false
        )
        AwLog.d("getUserRemark: " + userRemark)
        if (AwDataUtil.isEmpty(MyApp.transactionData)) {
            AwRecyclerViewUtil.resetView(mActivity, mAdapter, binding!!.rcvData)
        } else {
            mlist = AwGsonUtil.fromJson(
                MyApp.transactionData,
                object : TypeToken<List<TransactionItemBean?>?>() {}.type
            )
            mAdapter!!.addAllData(mlist)
        }

//        mlist = DataUtil.getDefaultTransactionList();
//        mAdapter.addAllData(mlist);
    }

    override fun initListener() {
        super.initListener()
        mAdapter!!.onItemClickListener =
            BaseQuickAdapter.OnItemClickListener { adapter: BaseQuickAdapter<*, *>, view: View?, position: Int ->
                val itemBean = adapter.getItem(position) as TransactionItemBean?
                toClass(
                    CommonImgActivity::class.java,
                    false,
                    Extras.params,
                    itemBean!!.list,
                    Extras.params_int,
                    0
                )
            }
    }
}